/*
 * FQNDescription.java
 * 
 * last update: 01.02.2010 by Florea Mihai
 * 
 * author:	Alec(panovici@elcom.pub.ro)
 * 			Florea Mihai(florea.mihai@gmail.com)
 * 
 * Obs:
 */
package engine;

import java.io.Serializable;
/**
 * A Fully Qualified Name (FQN) is
 * represented as a simple list,
 * with strings or index expressions as nodes.
 */

@SuppressWarnings("serial")
class FQNDescription implements Serializable{
	public Object node;
	public FQNDescription next;

	public FQNDescription (String node) {
		this.node = node;
		next = null;
	}

	public FQNDescription (ExpressionDescription node) {
		this.node = node;
		next = null;
	}

	public int len() {
		if (next == null) return 1;
		return 1 + next.len();
	}

	public void add(FQNDescription next) {
		if(this.next == null) this.next = next;
		else      
			this.next.add(next);
	}

	public void add(String name){
		add (new FQNDescription (name));
	}

	public void add(ExpressionDescription ed) {
		add ( new FQNDescription (ed));
	}

	public boolean isIndex() {
		return node instanceof ExpressionDescription;
	}

	public boolean isName() {
		return node instanceof String;
	}

	public FQN instantiate (NameSpace ns) throws ParseException{
		Object data;
		try { //String ?
				data = (String) node;
		} catch (ClassCastException cex) { //ExpressionDesc
			ExpressionDescription ed = (ExpressionDescription) node;
			data = ed.instantiate(ns);
		}

		return new FQN(data, next == null ? null : next.instantiate(ns));
	}

	public String toString () {
		return (node instanceof String ? node : "[" + node + "]") +
		(next == null ? "" : (next.isIndex() ? "" : ".") + next.toString());
	}
}
